package com.hao.study.controller;

import com.github.pagehelper.PageInfo;
import com.hao.study.common.APIResponse;
import com.hao.study.common.ResponeCode;
import com.hao.study.entity.Comment;
import com.hao.study.permission.Access;
import com.hao.study.permission.AccessLevel;
import com.hao.study.service.CommentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author: author
 * @description:    评论
 * @date: 2024/6/12 13:49
 * @version: 1.0
 */
@Api(tags={"评论管理接口"})
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentService service;

    @GetMapping("/list")
    public APIResponse list(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Comment> list =service.getCommentList(pageNum,pageSize);
        return new APIResponse<>(ResponeCode.SUCCESS,"查询成功",list);
    }

    @PostMapping("/create")
    @Transactional
    public APIResponse create(Comment comment){
        service.createComment(comment);
        return new APIResponse<>(ResponeCode.SUCCESS,"创建成功");
    }

    // 所有评论
    @GetMapping("/listThingComments")
    public APIResponse listThingComments(@RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize,
                                         String thingId, String order){
        PageInfo<Comment> list =  service.getThingCommentList(pageNum,pageSize,thingId, order);
        return new APIResponse(ResponeCode.SUCCESS, "查询成功", list);
    }

    // 用户的所有评论
    @GetMapping("listUserComments")
    public APIResponse listUserComments(@RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        String userId){
        PageInfo<Comment> list =  service.getUserCommentList(pageNum,pageSize,userId);
        return new APIResponse(ResponeCode.SUCCESS, "查询成功", list);
    }
    @Access(level = AccessLevel.ADMIN)
    @PostMapping("/delete")
    public APIResponse delete(String ids){
        System.out.println("ids===" + ids);
        // 批量删除
        String[] arr = ids.split(",");
        for (String id : arr) {
            service.deleteComment(id);
        }
        return new APIResponse(ResponeCode.SUCCESS, "删除成功");
    }
    @Access(level = AccessLevel.ADMIN)
    @PostMapping("/update")
    @Transactional
    public APIResponse update(Comment comment) throws IOException {
        service.updateComment(comment);
        return new APIResponse(ResponeCode.SUCCESS, "更新成功");
    }

    @PostMapping("/like")
    @Transactional
    public APIResponse like(String id) throws IOException {
        Comment commentBean = service.getCommentDetail(id);
        Integer likeCount = commentBean.getLikeCount()==null ? 1 : commentBean.getLikeCount() + 1;
        commentBean.setLikeCount(likeCount);
        service.updateComment(commentBean);
        return new APIResponse(ResponeCode.SUCCESS, "更新成功");
    }
}
